package com.m3958.firstgwt.server.accesschecker;


import com.m3958.firstgwt.client.types.SmartSubOperationName;
import com.m3958.firstgwt.server.model.Vote;
import com.m3958.firstgwt.server.model.WebSite;
import com.m3958.firstgwt.server.types.ObjectOperation;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartConstants;





public class VoteChecker extends BaseChecker<Vote> {

	@Override
	protected boolean canAdd() {
		int vid = reqPs.getParentId();
		if(vid == SmartConstants.NONE_EXIST_MODEL_ID){
			return true;
		}else{
			Vote pv = emp.get().find(Vote.class, vid);
			if(rso.isCreator(pv)){
				return true;
			}			
		}

		return hasObjectPerms(WebSite.class.getSimpleName(), getSiteIdFromPara(), ObjectOperation.ADD_CONTENT);
	}

	@Override
	protected boolean canCustom() {
		return false;
	}

	@Override
	protected boolean canFetch() {
		
		int siteId = reqPs.getIdValue(CommonField.SITE_ID.getValue());
		if(siteId == -1 && getModel() != null){
			siteId = getModel().getSiteId();
		}
		if(isSiteEditor(siteId) || isSiteOwner(siteId))return true;
		
		switch (getSubOpName()) {
		case FETCH_CHILDREN:
			if(reqPs.getParentId() == SmartConstants.NONE_EXIST_MODEL_ID)return true;
			return hasTreeContainerPerms(reqPs.getModelName(), reqPs.getParentId(), ObjectOperation.LIST_CHILDREN);
		default:
			return false;
		}
		
//		if(reqPs.getFetchOwner() != null && reqPs.getFetchOwner()){
//			return true;
//		}
//		if(SmartSubOperationName.FETCH_CHILDREN.toString().equals(reqPs.getSubOpType())){
//			if(reqPs.getIdValue("siteId") == SmartConstants.NONE_EXIST_MODEL_ID){
//				int vid = reqPs.getParentId();
//				Vote pv = emp.get().find(Vote.class, vid);
//				if(pv == null)return false;
//				if(rso.isCreator(pv)){
//					return true;
//				}			
//			}else{
//				return isObjectOwner();
//			}
//		}
//		return hasObjectPerms(WebSite.class.getSimpleName(), getSiteIdFromPara(), ObjectOperation.FETCH_CONTENT);
	}

	@Override
	protected boolean canMyRpc() {
		return false;
	}

	@Override
	protected boolean canRemove() {
		if(rso.isCreator(getModel())){
			return true;
		}
		return hasObjectPerms(WebSite.class.getSimpleName(), getSiteIdFromPara(), ObjectOperation.REMOVE_CONTENT);
	}

	@Override
	protected boolean canUpdate() {
		if(rso.isCreator(getModel())){
			return true;
		}
		return hasObjectPerms(WebSite.class.getSimpleName(), getSiteIdFromPara(), ObjectOperation.UPDATE_CONTENT);
	}

}
